package com.woniu.springbootshiro.service;

import com.woniu.springbootshiro.mapper.RoleMapper;
import com.woniu.springbootshiro.mapper.UserMapper;
import com.woniu.springbootshiro.pojo.Role;
import com.woniu.springbootshiro.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<String> selectPermissionByUserId(Long userId){
        User user = userMapper.selectUserByIdWithRoles(userId);
        List<String> permissionList = new LinkedList<>();
        List<Role> roleList = user.getRoleList();
        for (Role role : roleList) {
            List<String> permissions = role.getPermissionList().stream()
                    .map(permission -> permission.getPermission()).collect(Collectors.toList());
            permissionList.addAll(permissions);
        }
        return permissionList;
    }

    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }
}
